import {
    ANONYMOUS,
    API,
    HEADER_AUTHENTICATE, I18N_PREFIX,
    RESULT_CODE_ERROR_NOT_AUTH
} from "~~/server/resources/constants/constant";
import {consola} from "consola";
import {ResponseResult} from "~~/server/resources/types/response-result";

export default defineEventHandler(async (event) => {
    const url = getRequestURL(event);
    const clientHeaders = getHeaders(event);
    const authorization = clientHeaders[HEADER_AUTHENTICATE.toLowerCase()];
    if (url.pathname.startsWith(API) && url.href.indexOf(ANONYMOUS) < 0 && !authorization) {
        consola.error("请求路径,权限不足", url.pathname);
        const t = await useTranslation(event);
        return new ResponseResult(RESULT_CODE_ERROR_NOT_AUTH, url.pathname, t(I18N_PREFIX+RESULT_CODE_ERROR_NOT_AUTH));
    }
})